#include "gx_split.h"
#include <stdio.h>
#include <string.h>

int split(char * const str, char ** const to, const int row_num, const int col_num, const char c)
{
	if(str == NULL || *str == 0)
		return 0;
	if(to == NULL)
		return 0;

	char* delimiter = NULL;
	char *tmp = str;
	char *const array = reinterpret_cast<char*>(to);
	
	int i=0;

	while(i<row_num-1)
	{
		delimiter = strchr(str,c);

		if(delimiter == NULL)
			break;

		*delimiter = 0;
		strncpy(array+i*col_num,tmp,col_num-1);
		*(array+(i+1)*col_num-1)=0;
		*delimiter = c;
		tmp=delimiter+1;
		i++;
	}

	if(*tmp && i < row_num)
	{
		strncpy(array+i*col_num, tmp, col_num-1);
		*(array+(i+1)*col_num-1)=0;

		++i;
	}

	return i;
}
